﻿var DEFAULT_COMMENT = "tulis komentar...";
var DEFAULT_WALLPOST = "tulis wall temanmu...";

var DEFAULT_STATUS = "ada info apa hari ini?";

DeleteFeed = function(id)
{
    if(confirm("delete this feed?"))
    {
        var pnl = $id(prefix + "pnlUpdate");
        try{
        StartLoadingAnimation(pnl);
        }
        catch(err)
        {}
        cbkUpdate_c.PerformCallback("DELETE" + DELIMITER + id);
    }
    
}

UpdateStatus = function(){
    var status = $id("taStatus").value;
    var id = $id(prefix + "idNewest").innerHTML;
    var pnl = $id(prefix + "pnlUpdate");

    StartLoadingAnimation(pnl);
    cbkUpdate_c.PerformCallback("UPDATE_STATUS" + DELIMITER + status + DELIMITER + id);
}

tmrNewsFeed_c_Tick = function(s,e){
    var id = $id(prefix + "idNewest").innerHTML;
    var pnl = $id(prefix + "pnlUpdate");

    StartLoadingAnimation(pnl);
    cbkUpdate_c.PerformCallback(id);
}
cbkUpdate_c_CallbackComplete = function(e){
    var id = $id(prefix + "idNewest");
    var div = $id(prefix + "pnlNewsFeed");
    var pnl = $id(prefix+ "pnlUpdate");
    

    var splitted = e.result.split(DELIMITER);
    var params = e.parameter.split(DELIMITER);
    
    if(params[0] == "DELETE")
    {
        var tbl = $id("divnf_" + params[1]);
        tbl.parentNode.removeChild(tbl);
        try{
        EndLoadingAnimation(pnl);
        }
        catch(err)
        {}
        return;
    }
    
    id.innerHTML = splitted[0];
    
    if( div.insertAdjacentHTML )
    {
        div.insertAdjacentHTML( "afterBegin", splitted[1] );
    }
    else
    {
        var div2 = document.createElement('div');
        div2.innerHTML = splitted[1];
        
        div.insertBefore(div2, div.firstChild);
    }
    
    EndLoadingAnimation(pnl);
    
    
    if(params[0] == "UPDATE_STATUS")
    {
        var status = $id("taStatus");
        status.value = "";
    }
}

OlderPost_OnClick = function(){
    var id = $id(prefix + "idOldest").innerHTML;
    var pnl = $id("pnlOlder");
    
    StartLoadingAnimation(pnl);
    cbkOlder_c.PerformCallback(id);
    return false;
}
cbkOlder_c_CallbackComplete = function(e){
    var id = $id(prefix + "idOldest");
    var div = $id(prefix + "pnlNewsFeed");
    var pnl = $id("pnlOlder");

    var splitted = e.result.split(DELIMITER);
    
    id.innerHTML = splitted[0];
    //div.innerHTML = div.innerHTML + splitted[1];
    
    if( div.insertAdjacentHTML )
    {
        div.insertAdjacentHTML( "beforeEnd", splitted[1] );
    }
    else
    {
        var div2 = document.createElement('div');
        div2.innerHTML = splitted[1];        
        div.appendChild(div2);
    }
    
    EndLoadingAnimation(pnl);    
}

OpenComment = function(link, cid){

    var divcomm = $id("divcom_" + cid);
    var trimmed = trim(link.innerHTML);
    
    if( link.innerHTML != trimmed )
    {
        return false;
    }
    
    link.innerHTML += " ";
    
    if( divcomm.insertAdjacentHTML )
    {
        divcomm.insertAdjacentHTML( "beforeEnd", "<img src='" + LOADING.src + "' alt='' />" );
    }
    else
    {
        var div2 = document.createElement('div');
        div2.innerHTML = "<img src='" + LOADING.src + "' alt='' />";
        divcomm.appendChild(div2);
    }
        
    cbkMain_c.PerformCallback(cid);
}
cbkMain_c_CallbackComplete = function(e){
    var divcomm = $id("divcom_" + e.parameter);
    var frcomm = $id("frcom_" + e.parameter);
    
    var splitted = e.result.split(DELIMITER);
    divcomm.innerHTML = splitted[0];
    frcomm.innerHTML = splitted[1];
}

SendComment = function(link, cid){
    var tx = $id("txcom_" + cid);
    
    
    if( trim(tx.value) == "" )
    {
        return false;
    }
    
    var all = $nm("bt_com");
    
    for( var i=0; i < all.length; i++ )
    {
        all[i].className = "btd";
        all[i].disabled = true;
    }
    
    cbkComment_c.PerformCallback(cid + DELIMITER + tx.value);
    
    return false;
}
cbkComment_c_CallbackComplete = function(e){
    var splitted = e.parameter.split(DELIMITER);
    var result = e.result.split(DELIMITER);
    var divcomm = $id("divcom_" + splitted[0]);
    var tx = $id("txcom_" + splitted[0]);
    var commentcount = $id("divcmc_" + splitted[0]);
    
    if( divcomm.insertAdjacentHTML )
    {
        divcomm.insertAdjacentHTML( "beforeEnd", result[1] );
    }
    else
    {
        var div2 = document.createElement('div');
        div2.innerHTML = result[1];
        divcomm.appendChild(div2);
    }
    
    commentcount.innerHTML = result[0];
    
    tx.value = DEFAULT_COMMENT;
}

SetFocus = function(elmt){
    if( elmt.value == DEFAULT_COMMENT )
    {
        elmt.value = "";
    } 
    elmt.className = "cmTA2";   
    
    var btn = $id(elmt.id.replace("txcom_", "bt_"));
    btn.disabled = false;
    btn.className = "btn";    
}

SetBlur = function(elmt){
    if( trim(elmt.value) == "" )
    {
        elmt.value = DEFAULT_COMMENT;
    
        var btn = $id(elmt.id.replace("txcom_", "bt_"));
        btn.disabled = true;
        btn.className = "btd";
    }
    elmt.className = "cmTA";        
}

taStatus_Focus = function(elmt){
    if( elmt.value == DEFAULT_STATUS || elmt.value == DEFAULT_WALLPOST )
    {
        elmt.value = "";
    } 
    elmt.className = "taStatus2";   
    
    var btn = $id("btStatus");
    btn.disabled = false;
    btn.className = "btn";    
}

taStatus_Blur = function(elmt){
    if( trim(elmt.value) == "" )
    {
        elmt.value = DEFAULT_STATUS;
    
        var btn = $id("btStatus");
        btn.disabled = true;
        btn.className = "btd";
    }
    elmt.className = "taStatus";        
}

showMorePost = function(id)
{
    var div = $id("divSimilar_" + id);
    var tbl = $id("tblSimilar_" + id);
    
    div.style.display = "";
    tbl.style.display = "none";
}
toggle = function (fsid)
{
    var div = $id("div_" + fsid);
    var lnk = $id("lnk_" + fsid);
    
    var display = "";
    
    if( div.style.display != "none" )
    {
        display = "none";
    }
    
    div.style.display = display;
    
    if( lnk != null )
    {
        lnk.style.display = display;
    }
}

StartLoadingAnimation = function(div){
    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    
}

EndLoadingAnimation = function(div){
    div.innerHTML = "";    
}


BlockFeed_FromCompany = function(cid, tableid){
    
    var x = confirm("Block this Customer feeds?");
    
    if(x)
    {
        BlockFeed("BLOCKCUSTOMER", cid, tableid);
    }    
}

BlockFeed_FromUser = function(uid, tableid){
    
    var x = confirm("Block this User feeds?");
    
    if(x)
    {
        BlockFeed("BLOCKUSER", uid, tableid);
    }    
}

BlockFeed = function(action, uid, tableid)
{
    var url = "ASHX/NewsFeed.ashx?action=" + action + "&bid=" + uid;    var ajx = sendAJAX(url,true);
    ajx.onreadystatechange = function() {        if (ajx.readyState == 4) {                    if( ajx.responseText == "" )
            {
                var table = $id(tableid);
                table.parentNode.removeChild(table);
            }
        }    }
}